package com.newasia.management.controller;

import com.newasia.commons.result.ResponseData;
import com.newasia.commons.result.ResponseUtil;
import com.newasia.management.ISysMenuService;
import com.newasia.management.constants.ManagementRetCode;
import com.newasia.management.dto.user.menu.*;
import com.newasia.management.form.SysMenuForm;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/management")
public class MenuController {

    @Reference(timeout = 3000)
    ISysMenuService sysMenuService;

    @GetMapping("/menu")
    public ResponseData selectSysMenuList(SysMenuForm form){
        SysMenuListRequset requset = new SysMenuListRequset();
        requset.setMenuId(form.getMenuId());
        requset.setStatus(form.getStatus());
        SysMenuListResponse response = sysMenuService.selectSysMenuList(requset);
        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response);
        }
        return new ResponseUtil().setErrorMsg(response);
    }

    @PostMapping("/menu")
    public ResponseData insertSysMenu(@RequestBody SysMenuForm form){
        InsertSysMenuRequset requset = new InsertSysMenuRequset();
        requset.setMenuName(form.getMenuName());
        requset.setPerms(form.getPerms());
        InsertSysMenuResponse response = sysMenuService.insertSysMenu(requset);
        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response);
        }
        return new ResponseUtil().setErrorMsg(response);
    }

    @PutMapping("/menu/{mid}")
    public ResponseData updateSysMenu(@RequestBody SysMenuForm form,@PathVariable("mid") Long mid){
        UpdateSysMenuRequset requset = new UpdateSysMenuRequset();
        requset.setMenuId(mid);
        requset.setMenuName(form.getMenuName());
        requset.setPerms(form.getPerms());
        UpdateSysMenuResponse response = sysMenuService.updateSysMenu(requset);
        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response);
        }
        return new ResponseUtil().setErrorMsg(response);
    }

    @DeleteMapping("/menu/{mid}")
    public ResponseData login(@PathVariable("mid") Long mid){
        DeleteSysMenuRequset requset = new DeleteSysMenuRequset();
        requset.setMenuId(mid);
        DeleteSysMenuResponse response = sysMenuService.deleteSysMenu(requset);
        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response);
        }
        return new ResponseUtil().setErrorMsg(response);
    }
}
